package com.xianbeiying.app.ckgl.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.NoticeListdapter;
import com.xianbeiying.app.ckgl.bean.NoticesBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;

/**
 * 公告
 */
public class NoticeFragment  extends BaseFragment  implements HttpCallBackUtil{

    private static NoticeFragment noticeFragment;
    public static NoticeFragment getInstance(){
        noticeFragment=new NoticeFragment();
        return noticeFragment;
    }
    private View view;
    private GetModel getModel;
    private ImageView iv_title_left;
    private TextView tv_title;
    private ListView notice_list;
    private NoticeListdapter noticelistAdpter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       view=inflater.inflate(R.layout.fragment_notice,null);
        initView();
        return view;
    }

    private void initView() {
        getModel=new GetModel(getActivity());
        getModel.addResponseListener(this);
        tv_title=(TextView) view.findViewById(R.id.tv_title);
        iv_title_left=(ImageView) view.findViewById(R.id.iv_title_left);
        notice_list=(ListView) view.findViewById(R.id.notice_list);
        iv_title_left.setVisibility(View.GONE);
        initData();
        initEvent();

    }
    private void initData() {
        tv_title.setText("公告");
        if(MyApplication.noticesBean!=null){
            noticesBean=MyApplication.noticesBean;
            setContent();
        }else{
            getModel.GETNotice("list",true);
        }
    }
    private void initEvent() {
        iv_title_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                getActivity().finish();
            }
        });
    }
   private NoticesBean noticesBean;
    @Override
    public void onCallBackData(String url,Object content) {
      if(url.equals("公告信息")){
          if(content!=null){
              noticesBean= JSON.parseObject(new String((byte []) content),NoticesBean.class);
              MyApplication.noticesBean=noticesBean;
              setContent();
          }
      }
    }

    private void setContent() {
        if(noticesBean.getNOTICE()!=null&&noticesBean.getNOTICE().size()!=0){
            noticelistAdpter=new NoticeListdapter(getActivity(),noticesBean);
            notice_list.setAdapter(noticelistAdpter);
        }
    }
}
